home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 007a / cug315.zip / MSC_GRPH.C < prev    next >
C/C++ Source or Header  |  1990-05-16  |  5KB  |  247 lines

  1. /* modified 11/89 by T Clune to accept y min/max arguments in msc_bar() */
  2. /* and msc_line() functions. */
  3.  
  4. /* msc_grph.c was written 4/89 by Tom Clune to support CGA, EGA, and */
  5. /* VGA operation with FTGRAPH.C.  It uses the Microsoft C GRAPHICS library */
  6. /* of graphics support routines, which REQUIRES large memory model ONLY. */
  7. /* Copyright (c) 1989, Eye Research Institute 20 Staniford St., */
  8. /* Boston, MA 02114.  All rights reserved */
  9.  
  10. #include "ansi.h"
  11. #include "msc_grph.h"
  12. #include "menu.h"
  13. #include "mouselib.h"
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <conio.h>
  18. #include <graph.h>
  19.  
  20. static void bar_plot(), line_plot();
  21.  
  22. static struct videoconfig graph_mode;
  23.  
  24.  
  25. static int xmax;
  26. static double y_min, y_max;
  27. static int x_low = 64;       /* x,y axis limits */
  28. static int x_high = 576;
  29. static int xtick_inc = 64;
  30. static int y_low ;
  31. static int y_high;    /* y is inverted, so re-invert */
  32.  
  33. static int valid_mode=0;
  34.  
  35. #define MSX_SCALE(x)  int_scale(x, 0, xmax, x_low, x_high)  /* a readable scaling */
  36. #define MSY_SCALE(y)  dbl_scale(y, y_min, y_max, y_low, y_high)  /* for x and y */
  37.  
  38.  
  39. int get_adaptor()
  40. {
  41.     _getvideoconfig( &graph_mode);
  42.  
  43.     if(graph_mode.adapter== _CGA)
  44.     {
  45.     valid_mode= _HRESBW;
  46.     y_low=190;
  47.     y_high=15;
  48.     }
  49.     if(graph_mode.adapter== _EGA)
  50.     {
  51.     valid_mode= _HRES16COLOR;
  52.     y_low= 190;
  53.     y_high=15;
  54.     }
  55.     if(graph_mode.adapter== _VGA)
  56.     {
  57.     valid_mode= _VRES2COLOR;
  58.     y_low=460;
  59.     y_high=30;
  60.     }
  61.     if(graph_mode.adapter== _HGC)
  62.     {
  63.     valid_mode= _HERCMONO;
  64.     y_low=300;
  65.     y_high=15;
  66.     }
  67.  
  68.     return graph_mode.adapter;
  69.  
  70. }
  71.  
  72.  
  73.  
  74. /* the main routine for drawing a bar graph */
  75.  
  76. void msc_bar(data, n, minval, maxval)
  77. double data[];
  78. int n;
  79. double minval, maxval;
  80. {
  81.     int i;
  82.  
  83.     if(!valid_mode)
  84.     {
  85.     CLS;
  86.     printf("This program does not support screen graphics on this computer.\n");
  87.     printf("Press any key to retrun to previous menu\n");
  88.     if(get_mouse_flag())
  89.         inpause();
  90.     else
  91.         getch();
  92.     return;
  93.     }
  94.  
  95.  
  96.     xmax=n;
  97.     y_min=minval;
  98.     y_max=maxval;
  99.  
  100.     _setvideomode(valid_mode);
  101.     _clearscreen(_GCLEARSCREEN);
  102.  
  103.     _moveto(x_low, y_low);
  104.     _lineto(x_high, y_low);    /* draw the x-axis */
  105.  
  106.     for(i=x_low;i<=x_high;i+=xtick_inc)               /* draw x-axis tick marks */
  107.     {
  108.     _moveto(i,y_low);
  109.     _lineto(i, y_low+5);
  110.     }
  111.  
  112.     for(i=0;i<n;i++)
  113.     bar_plot(i,data[i]);
  114.  
  115.     _settextwindow(1,1,1,80);
  116.     _settextposition(1,1);
  117.     printf("Press any key to exit graphics mode");
  118.     if(get_mouse_flag())
  119.     inpause();
  120.     else
  121.     getch();
  122.  
  123.     _setvideomode(_DEFAULTMODE);
  124.     CLS;
  125.  
  126. }
  127.  
  128.  
  129. /* the main routine for drawing a line graph */
  130.  
  131. void msc_line(data, n, minval, maxval)
  132. double data[];
  133. int n;
  134. double minval, maxval;
  135. {
  136.     int i;
  137.  
  138.     if(!valid_mode)
  139.     {
  140.     CLS;
  141.     printf("This program does not support screen graphics on this computer.\n");
  142.     printf("Press any key to retrun to previous menu\n");
  143.     if(get_mouse_flag())
  144.         inpause();
  145.     else
  146.         getch();
  147.     return;
  148.     }
  149.  
  150.  
  151.     xmax=n;
  152.     y_min=minval;
  153.     y_max=maxval;
  154.  
  155.     _setvideomode(valid_mode);
  156.     _clearscreen(_GCLEARSCREEN);
  157.  
  158.     _moveto(x_low, y_low);
  159.     _lineto(x_high, y_low);    /* draw the x-axis */
  160.  
  161.     _moveto(x_low, y_low);
  162.     _lineto(x_low, y_high);    /* draw the y-axis */
  163.  
  164.     for(i=y_low;i>=y_high;i-=60)                 /* draw y-axis tick marks */
  165.     {
  166.     _moveto(x_low, i);
  167.     _lineto(x_low-5, i);
  168.     }
  169.  
  170.     for(i=x_low;i<=x_high;i+=xtick_inc)               /* draw x-axis tick marks */
  171.     {
  172.     _moveto(i,y_low);
  173.     _lineto(i, y_low+5);
  174.     }
  175.  
  176.     for(i=0;i<n;i++)
  177.     line_plot(i,data[i]);
  178.  
  179.     _settextwindow(1,1,1,80);
  180.     _settextposition(1,1);
  181.     printf("Press any key to exit graphics mode");
  182.     if(get_mouse_flag())
  183.     inpause();
  184.     else
  185.     getch();
  186.  
  187.     _setvideomode(_DEFAULTMODE);
  188.     CLS;
  189.  
  190. }
  191.  
  192.  
  193.  
  194. /* bar_plot() is draws the bars for the bar graph */
  195.  
  196. static void bar_plot(x_val, y_val)
  197. int x_val;
  198. double y_val;
  199.  
  200. {
  201.  
  202.     int x,y;   /* screen values for plot */
  203.     static int y_base;   /* previous x,y value */
  204.  
  205.     x = MSX_SCALE(x_val);    /* scale the point for the screen */
  206.     y = MSY_SCALE(y_val);
  207.     y_base=MSY_SCALE(y_min);
  208.     _moveto(x,y);
  209.     _lineto(x,y_base);
  210.  
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217. /* line_plot() draws the x,y data for the line graph */
  218.  
  219. static void line_plot(x_val, y_val)
  220. int x_val;
  221. double y_val;
  222.  
  223. {
  224.  
  225.     int x,y;   /* screen values for plot */
  226.     static int x_old, y_old;   /* previous x,y value */
  227.     if(x_val <= 0)  /* if this is the beginning of a new line plot */
  228.     x_old = -1;     /* mark it as such */
  229.  
  230.  
  231.     x = MSX_SCALE(x_val);    /* scale the point for the screen */
  232.     y = MSY_SCALE(y_val);
  233.     if(x_old == -1)        /* if this is the beginning of a new line */
  234.     {
  235.     x_old = x;        /* set the old values to only draw a point */
  236.     y_old = y;
  237.     }
  238.     _moveto(x_old, y_old);
  239.     _lineto(x,y);
  240.  
  241.     x_old = x;  /* assign the x,y values to the "last point" variables */
  242.     y_old = y;  /* in preparation for the next pass */
  243.  
  244.  
  245. }
  246.  
  247.